import {
  isUndefined,
  isFunction,
  isAsyncFunction,
} from "@/utils/typeInspector";

export function useDispatch(reducers = {}) {
  // TODO: 纪丹蕾 挑战性 函数中的reducers 需要遍历验证是否为函数
  return function dispatch(action: string, ...args: unknown[]) {
    const reducer = reducers[action];
    if (isUndefined(reducer)) {
      throw new Error("No action related reducer was found!");
    }
    if (!isFunction(reducer) && !isAsyncFunction(reducer)) {
      throw new Error("reducer is not a function");
    }
    return reducer.call(this, ...args);
  };
}
